package com.hashcoding.eyes.ui.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.FitCenter;
import com.hashcoding.eyes.R;
import com.hashcoding.eyes.data.entity.Photo;
import com.hashcoding.eyes.module.utils.LogUtil;
import com.hashcoding.eyes.ui.widget.DrawableCenterTextView;

import butterknife.Bind;
import butterknife.ButterKnife;
import jp.wasabeef.glide.transformations.CropCircleTransformation;

/**
 * PhotosListAdapter
 *
 * @author Maochuan
 * @date 2016-02-03
 */
public class PhotosListAdapter extends BaseListAdapter<Photo> {

    public PhotosListAdapter(Context context) {
        super(context);
    }

    @Override
    protected View getItemView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null || convertView.getTag() == null) {
            convertView = mInflater.inflate(R.layout.vm_photo_item_layout, parent, false);
            holder = new ViewHolder(convertView);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Photo photo = getItem(position);
        Glide.with(mContext).load(photo.url)
                .placeholder(R.color.default_image)
                .bitmapTransform(new CenterCrop(mContext))
                .into(holder.mImageView);

        holder.mPhotoNameView.setText(photo.title);
        holder.mPhotoDescriptionView.setText(photo.description);
        return convertView;
    }

    /**
     * This class contains all butterknife-injected Views & Layouts from layout file 'vm_photo_item_layout.xml'
     * for easy to all layout elements.
     *
     * @author ButterKnifeZelezny, plugin for Android Studio by Avast Developers (http://github.com/avast)
     */
    class ViewHolder {
        @Bind(R.id.image_view)
        ImageView mImageView;
        @Bind(R.id.photo_name_view)
        TextView mPhotoNameView;
        @Bind(R.id.photo_description_view)
        TextView mPhotoDescriptionView;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
            view.setTag(this);
        }
    }
}
